package com.zj.main

import android.content.Context
import com.xuexiang.xutil.XUtil.runOnUiThread
import com.xuexiang.xutil.tip.ToastUtils.toast
import com.zj.easy.image.Image.with
import com.zj.easy.utils.app.AppUtils.isDebug
import com.zj.easy.utils.log
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.math.BigDecimal
import java.text.DecimalFormat
import kotlin.math.pow

object Demo2 {

    @JvmStatic
    fun main(args: Array<String>) {
//        test3()
//        test4()
        test5()
    }

    private fun test5() {
        val b = java.lang.StringBuilder(128)

        b.append("Intent { ")
        b.append(" }")

        println("=========" + StringBuilder(128).toString())
    }

    fun test3() {
        var f1 = 0.3f

        var f2 = Integer.toBinaryString(f1.toRawBits()).toCharArray()
        var sum = 0.0.toBigDecimal()
        var j = 2
        println("=========" + Integer.toBinaryString(f1.toBits()))
        for (i in (f2.size - 24)..(f2.size - 1)) {
            var t = Integer.parseInt(f2[i].toString())
            if (i == f2.size - 24) {
                t = 1
            }
            if (t == 1) {
                sum = sum + (1.0.toBigDecimal()).divide(2.0.toBigDecimal().pow(j))
                    .multiply(10000000.0.toBigDecimal())
            }
//            sum=sum+(1.0).div(2.0.pow(j))*t
            println("=========" + sum)
            j++
        }
        println("=========" + sum)
        println("=======--${BigDecimal(0.3f.toDouble())}")
        println("=======--${0.3f.toDouble()}")
        println("=======--${0.3f.toDouble().toFloat()}")
        println("=======--${0.30000001f == 0.30000002f}")
        println("=======--${0.30000001f == 0.30000003f}")
        println("=======--${0.30000002f}")
        println("=======--${0.30000003f}")
        println("=======--${0.30000004f}")
        println("=======--${0.29999999f + 0.00000002f}")
        println("=======--${0.29999999f + 0.00000003f}")
        println("=======--${0.29999999f + 0.00000004f}")
        println("=======--${0.29999999f + 0.00000005f}")
        println("=======--${0.3f * 10000000 - 3000000.0}")
    }


    private fun test4() {
        var f1: Float = 2599.2F
        var f2: Float = 1110.9F
        var f3 = 0.2999999999999999388f
        var f7 = 0.2999999999999999388

        var f4 = 2.0
        var f6 = 0.3
        var f9 = f7.toBits()
        for (j in 0..10) {
            for (i in 0..10) {
                var f5 = f3 + i * f4 / (2.0.pow(30))
                ("${f5 == f6}======${i}======${f5.toBits()}").log()
            }
        }
        ("============${BigDecimal(0.3).toString()}").log()
    }

    fun test1(iv: Context?) {
        with(iv!!)


        isDebug()
        isDebug()
    }

    private fun test2() {
//        var f1=11112.0
//        var f2=11111.9
//        var f1="1.0".toFloat()
//        var f2="0.9".toFloat()
        var f1: Float = 2599.2F
        var f2: Float = 1110.9F
        var f3 = 123456789f
        var f7 = 0.2999999999999999388

        var f4 = 2.0
        var f6 = 0.3
        var f9 = f7.toBits()
        for (j in 0..100) {
            ("============${Double.fromBits(f9++)}").log()
//            for (i in 0..10) {
//                ("============${ Double.fromBits(f8++)}").log()
//            var f5 = f3 + i*f4 / (2.0.pow(60))
//            ("${f5==f6}======${i}======${f5.toBits()}").log()
//            ("============${1.0.toBigDecimal().divide(2.0.toBigDecimal().pow(i)).toPlainString()}").log()
//            }
        }

        ("============${f1 - f2}").log()
        ("============${(0.3 - 0.2) == 0.1}").log()
        ("============${DecimalFormat.getInstance().format(Int.MAX_VALUE / 100.0F)}").log()
        var bd1 = BigDecimal(f1.toString())
        var bd2 = BigDecimal(f2.toString())
        ("============${(bd1 - bd2).setScale(1)}").log()
        ("============${2.0.pow(-7)}").log()

//        for (i in 0..99) {
//            comput(Int.MAX_VALUE / 10000 * i, Int.MAX_VALUE / 10000 * (i + 1))
//        }

    }

    private var num = Int.MAX_VALUE
    private fun comput(i: Int, i1: Int) {
//        var big=BigDecimal(100)
        GlobalScope.launch(Dispatchers.IO) {
//            var time=System.currentTimeMillis()
            val length = i1.toString().length + 1
            for (i in i..i1) {
                var re = (i / 100.0).toString()
//                if (i / 100.0 != BigDecimal(i).divide(big).toDouble()) {

//                ("=======----=====${i}").log()
                if (re.length > length) {
                    runOnUiThread { toast(("===${i}")) }
                } else {
                    ("===${num}===${re}").log()
                }
                num--
//                ("===${num}").log()
            }
//            ("=======----=====${System.currentTimeMillis()-time}").log()
        }
    }

}